package ex3.taskqueue;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import ex3.Constants;

public class TaskFileReader implements Runnable {
	public TaskFileReader (FileReader freader){
		reader = freader;
	}
	
	public void run() {
		BufferedReader br = new BufferedReader(reader);
		String str;
		
		try {
			while ((str = br.readLine()) != null){
				if (str.trim().isEmpty())
					continue;
				
				if (str.trim().charAt(0) == '#')
					continue;
				
				//	Read the line and add the task
				String[] inputLine = str.split(Constants.DELIMITER);
				int opID = Integer.parseInt(inputLine[0]);
				char op = inputLine[1].charAt(0);
				int op1 = Integer.parseInt(inputLine[2]);
				int op2 = Integer.parseInt(inputLine[3]);
				TaskQueueImpl.taskQueue.AddTask(op, opID, op1, op2, false);
			}
		} catch (IOException e) {
			System.out.println("IOException: " + e.getMessage());
			e.printStackTrace();
		}
		
		//	Set finished reading from file flag
		TaskQueueImpl.taskQueue.SetFinishedReading();
		TaskQueueImpl.taskQueue.DestroyThread();
	}
	
	private FileReader reader;
}
